<?php
declare (strict_types = 1);

namespace app\common\service;

use app\common\model\AdminAdmin;
use think\facade\Request;
use app\common\model\SchoolCustomerWorker as M;
use app\common\validate\SchoolCustomerWorker as V;
use app\common\model\AdminAdmin as A;


class SchoolCustomerWorker
{
    // 添加
    public static function goAdd($data)
    {
        //验证
        $validate = new V;
        if(!$validate->scene('add')->check($data))
        return ['msg'=>$validate->getError(),'code'=>201];
        try {
            $password =  set_password($data['password']);
            $admin = session('admin');
            $admin_id = AdminAdmin::insertGetId(array_merge($data, [
                'password' => $password,
                'top_id' => $admin['id'],
                'school_id' => $admin['school_id'],
                'create_time'=>date('Y-m-d H:i:s'),
                'update_time'=>date('Y-m-d H:i:s'),
            ]));
            dataInsert('bl_admin_admin_role', [
                'admin_id' => $admin_id,
                'role_id' => 7
            ]);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }
    
    // 编辑
    public static function goEdit($data,$id)
    {
        $data['id'] = $id;
        //验证
        $validate = new V;
        if(!$validate->scene('edit')->check($data))
        return ['msg'=>$validate->getError(),'code'=>201];
        try {
            $model = AdminAdmin::find($id);
            if ($data['password']){
                $model->password = set_password($data['password']);
                $model->token = null;
            }
            AdminAdmin::update($data);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 状态
    public static function goStatus($data,$id)
    {
        $model =  M::find($id);
        if ($model->isEmpty())  return ['msg'=>'数据不存在','code'=>201];
        try{
            $model->save([
                'status' => $data,
            ]);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 删除
    public static function goRemove($id)
    {
        $model = A::find($id);
        if ($model->isEmpty()) return ['msg'=>'数据不存在','code'=>201];
        try{
           $model->delete();
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 批量删除
    public static function goBatchRemove($ids)
    {
        if (!is_array($ids)) return ['msg'=>'数据不存在','code'=>201];
        try{
            M::destroy($ids);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 获取列表
    public static function goRecycle()
    {
        if (Request::isPost()){
            $ids = Request::param('ids');
            if (!is_array($ids)) return ['msg'=>'参数错误','code'=>'201'];
            try{
                if(Request::param('type')){
                    $data = M::onlyTrashed()->whereIn('id', $ids)->select();
                    foreach($data as $k){
                        $k->restore();
                    }
                }else{
                    M::destroy($ids,true);
                }
            }catch (\Exception $e){
                return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
            }
            return ['msg'=>'操作成功'];
        }
        //按用户名
        $where = [];
        $limit = input('get.limit');
        
        $list = M::onlyTrashed()->where($where)->paginate($limit);
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }

    public static function customCount($class)
    {
        $limit = input('get.limit');
        $where = [];

        //按手机号查找
//        if ($mobile = input("mobile")) {
//            $where[] = ["mobile", "like", "%" . $mobile . "%"];
//        }
        //按姓名查找
        if ($username = input("username")) {
            $where[] = ["username", "like", "%" . $username . "%"];
        }
        //按客户意向查找
        if ($intention = input("intention")) {
            $where[] = ["intention", "like", "%" . $intention . "%"];
        }
        //按邀约状态查找
        if ($customer_type = input("customer_type")) {
            $where[] = ["customer_type", "=",  $customer_type];
        }

        //按客户来源查找
        if ($source_id = input("source_id")) {
            $where[] = ["source_id", "=",  $source_id];
        }
        $list = \app\common\model\RecruitCustomer::where('admin_id',$class)
            ->where($where)
            ->with('getSource')
            ->paginate($limit)
            ->each(function (&$item){
                $item['types'] = self::typeStatus()[$item['customer_type']];
        });
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }

    public static function typeStatus()
    {
        return [
            1=>'待跟进',
            2=>'已邀约',
            3=>'已到访',
            4=>'未到访',
            5=>'已成交',
            6=>'未成交'
        ];
    }
}
